package com.bd.rj.common.filters;

/**
 * @desc 打印请求参数过滤器
 * @author 白世俊
 * @datetime: 2024-12-26 11:00
 */

import cn.dev33.satoken.stp.StpUtil;
import jxl.write.DateTime;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.logging.SimpleFormatter;

/**
 * @desc 参数打印过滤器
 * @author 白世俊
 * @date 2025/1/9
 */
@WebFilter("/*")
public class PrintAllArgsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        Map<String, String[]> parameterMap = req.getParameterMap();
        System.out.println("**************本次请求参数如下***************");
        System.out.println("时间: "+ new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()));
        System.out.println("请求路径: " + req.getRequestURL());
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            //遍历打印请求参数
            String parameterName = entry.getKey();
            String[] parameterValues = entry.getValue();
            System.out.print("参数名: " + parameterName);
            if (parameterValues != null) {
                for (String value : parameterValues) {
                    System.out.println("\t参数值: " + value);
                }
            }
        }
//        if (req.getContentType().contains("json")) {
//            System.out.println("请求体内容:");
//            BufferedReader reader = req.getReader();
//            while (reader.ready()) {
//                System.out.println(reader.readLine());
//            }
//        }
        System.out.println("\n*******************************************");
        filterChain.doFilter(req, resp);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
